<?php
namespace App\Service\Xcx\Ywmall;

use App\Service\Xcx\BaseService as Service;

class GoodsService extends Service
{
    //商品

    //列表
    public static function index($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwGoods)->where('del', 0);
        if ($pas['uid']) {
//            $us = self::dbConn(self::$dbUserShop)
//                ->where('del', 0)
//                ->where('uid', $pas['uid'])
//                ->get();
//            if (count($us)) {
//                $shopids = [];
//                foreach ($us as $v) {
//                    $shopids[] = $v->shop_id;
//                }
//                $db = $db->whereIn('shop_id', $shopids);
//            }
            $db = $db->where('uid', $pas['uid']);
        }
        if ($pas['cate_id']) {
            $db = $db->where('cate_id', $pas['cate_id']);
        }
        if ($pas['shequ_id']) {
            $db = $db->where('shequ_id', $pas['shequ_id']);
        }
        $db = $db->where('num', '>', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return self::toback(-2, '没有数据', [], $total);
        }
        $ary = [];
        foreach ($res as $v) {
            $shop_id = $v->shop_id;
            $shop_name = '小店名称';
            if ($shop_id) {
                $shopOne = self::dbConn(self::$dbYwShop)
                    ->where('del', 0)
                    ->where('id', $shop_id)
                    ->first();
                if ($shopOne) {
                    $shop_name = $shopOne->title;
                }
            }
            $codeOne = self::dbConn(self::$dbQr)->where('code', $v->code)->first();
            $show_code = $codeOne&&$codeOne->show_code ? $codeOne->show_code : '';
            $ary[] = [
                'id' => $v->id, 'title' => $v->gname, 'detail' => $v->detail,
                'cover' => file_exists($v->cover) ? env('DOMAIN') . '/'. $v->cover : '',
                'code' => $v->code, 'show_code' => $show_code,
                'price' => toNumber($v->price), 'num' => $v->num,
                'price_old' => toNumber($v->price_old),
                'shop_id' => $v->shop_id,'shop_name' => $shop_name,
            ];
        }
        return self::toback(0, '', $ary, $total);
    }

//    //详情
//    public static function one($code)
//    {
//        $codeOne = self::dbConn(self::$dbQr)
//            ->where('code', $code)
//            ->first();
//        if (!$codeOne) {
//            return self::toback(-2, '码错误');
//        }
//        $res = self::dbConn(self::$dbYwGoods)
//            ->where('del', 0)
//            ->where('code', $code)
//            ->orderBy('id', 'desc')
//            ->first();
//        if (!$res) {
//            return self::toback(-2, '没有数据');
//        }
//        $res->cover = file_exists($res->cover) ? env('DOMAIN') . '/' . $res->cover : '';
//        $res->show_code = $codeOne->show_code;
//        $res->detail = mb_strlen($res->describe)>10 ? mb_substr($res->describe, 0, 10) . '...' : $res->describe;
//        $res->detail = $res->detail ? $res->detail : '-';
//        $cateOne = self::dbConn(self::$dbCate)->where('id', $res->cate_id)->first();
//        //分类
//        $cate_pid = $cateOne ? $cateOne->pid : 0;
//        $res->cate_name = $cateOne ? $cateOne->title : '-';
//        if ($cate_pid) {
//            $cate_name = $cateOne->title;
//            $catePone = self::dbConn(self::$dbCate)->where('id', $cate_pid)->first();
//            $cate_pname = $catePone ? $catePone->title : '-';
//            $res->cate_name = $cate_pname . '-' . $cate_name;
//        }
//        //浏览量+最近活跃
//        $res->visit_total = self::dbConn(self::$dbQrlog)->where('code', $code)->count();
//        $logOne = self::dbConn(self::$dbQrlog)
//            ->where('code', $code)
//            ->orderBy('id', 'desc')
//            ->first();
//        $res->lasttime = $logOne ? $logOne->addtime : '-';
//        //属性集合
//        $res->attrs = self::dbConn(self::$dbAttr)
//            ->select('id', 'attr_name', 'attr_val')
//            ->where('del', 0)
//            ->where('yw_type', 1)
//            ->where('yw_id', $res->id)
//            ->get();
//        return self::toback(0, '', $res);
//    }

    //详情
    public static function onebyid($id)
    {
        $res = self::dbConn(self::$dbYwGoods)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        if (!$res->num) {
            return self::toback(-2, '没有库存');
        }
        $codeOne = self::dbConn(self::$dbQr)
            ->where('code', $res->code)
            ->first();
        $res->gname = $res->gname ? $res->gname : '-';
        $res->price = toNumber($res->price);
        $res->price_old = toNumber($res->price_old);
        $res->cover = file_exists($res->cover) ? env('DOMAIN') . '/' . $res->cover : '';
        //浏览量+最近活跃
        $res->visit_total = self::dbConn(self::$dbQrlog)->where('code', $res->code)->count();
        $logOne = self::dbConn(self::$dbQrlog)
            ->where('code', $res->code)
            ->orderBy('id', 'desc')
            ->first();
        $res->lasttime = $logOne ? $logOne->addtime : '-';
        $res->show_code = $codeOne ? $codeOne->show_code : '-';
        $res->code = $res->code ? $res->code : '-';
        //属性集合
        $res->attrs = self::dbConn(self::$dbAttr)
            ->select('id', 'attr_name', 'attr_val')
            ->where('del', 0)
            ->where('yw_type', 2)
            ->where('yw_id', $id)
            ->get();
        return self::toback(0, '', $res);
    }

    //获取码
    public static function oneidByCode($code)
    {
        $gOne = self::dbConn(self::$dbYwGoods)
            ->where('code', $code)
            ->first();
        if (empty($gOne)) {
            return self::toback(-2, '没有商品');
        }
        return self::toback(0, '', $gOne->id);
    }

    //获取码
    public static function codeBySerial($serial)
    {
        $qrOne = self::dbConn(self::$dbQr)
            ->where('show_code', $serial)
            ->first();
        if (empty($qrOne)) {
            return self::toback(-2, '没有码');
        }
        if (!$qrOne->yw_id) {
            return self::toback(-2, '码没被使用');
        }
        $resBack = [
            'code' => $qrOne->code,
            'yw_type' => $qrOne->yw_type,
            'yw_id' => $qrOne->yw_id,
        ];
        return self::toback(0, '', $resBack);
    }

    //新增
    public static function add($pas, $whe)
    {
        $pas['shequ_id'] = 0;
        if (isset($whe['shequ']) && $whe['shequ']) {
            $sqOne = self::dbConn(self::$dbShequ)
                ->where('del', 0)
                ->where('title', $whe['shequ'])
                ->first();
            if (empty($sqOne)) {
                return self::toback(-2, '没有社区');
            }
            $pas['shequ_id'] = $sqOne->id;
        }
//        $codeOne = self::dbConn(self::$dbQr)
//            ->where('code', $pas['code'])
//            ->first();
//        if (empty($codeOne)) {
//            return self::toback(-2, '没有码');
//        }
//        if ($codeOne->yw_id) {
//            return self::toback(-2, '已被使用');
//        }
        $pas['addtime'] = $pas['uptime'] = toDate();
        $lastid = self::dbConn(self::$dbYwGoods)->insertGetId($pas);
        if ($pas['code']) {
            self::dbConn(self::$dbQr)
                ->where('code', $pas['code'])
                ->update(['yw_type' => 2, 'yw_id' => $lastid, 'usetime' => toDate()]);
        }
        self::dbConn(self::$dbImgTmp)
            ->where('link', $pas['cover'])
            ->update(['usetime' => toDate()]);
        return self::toback();
    }

    //修改
    public static function modify($id, $pas, $whe)
    {
        $gOne = self::dbConn(self::$dbYwGoods)->where('id', $id)->first();
        if (empty($gOne)) {
            return self::toback(-2, '没有数据');
        }
        $pas['uptime'] = toDate();
        self::dbConn(self::$dbYwGoods)
            ->where('id', $id)
            ->update($pas);
        return self::toback();
    }

    //修改图片
    public static function imgUpload($id, $img)
    {
        $gOne = self::dbConn(self::$dbYwGoods)->where('id', $id)->first();
        if (empty($gOne)) {
            return self::toback(-2, '没有数据');
        }
        if (file_exists($gOne->cover)) {
            unlink($gOne->cover);
        }
        self::dbConn(self::$dbYwGoods)->where('id', $id)->update(['cover' => $img]);
        return self::toback();
    }

    //分类获取
    public static function cateList()
    {
        $ary = [];
        $res = self::dbConn(self::$dbCate)
            ->where('del', 0)
            ->where('type', 1)
            ->get();
        if (!count($res)) {
            return self::toback(0, '', $ary);
        }
        foreach ($res as $v) {
            $pid = $v->pid;
            $cate_name = $v->title;
            if ($pid) {
                $pone = self::dbConn(self::$dbCate)
                    ->where('pid', $pid)
                    ->first();
                $cate_name = $pone ? $pone->title . '-' . $cate_name : $cate_name;
            }
            $ary[] = [
                'id' => $v->id, 'title' => $cate_name,
            ];
        }
        return self::toback(0, '', $ary);
    }

    //分类设置
    public static function cateEdit($id, $cateid)
    {
        self::dbConn(self::$dbYwGoods)
            ->where('id', $id)
            ->update(['cate_id' => $cateid]);
        $cateOne = self::dbConn(self::$dbCate)
            ->where('id', $cateid)
            ->first();
        $cate_name = $cateOne->title;
        if ($cateOne->pid) {
            $pOne = self::dbConn(self::$dbCate)
                ->where('id', $cateOne->pid)
                ->first();
            $cate_name = $pOne->title . '-' . $cate_name;
        }
        $arys = [
            'id' => $cateid, 'title' => $cate_name,
        ];
        return self::toback(0, '', $arys);
    }

    //属性获取
    public static function attrList($cate_id, $id)
    {
        $cateOne = self::dbConn(self::$dbAttr)
            ->where('pid', $cate_id)
            ->get();
        $gOne = self::dbConn(self::$dbYwGoods)
            ->where('id', $id)
            ->first();
        if (!$gOne) {
            return self::toback(-2, '没有商品码');
        }
        $arys = [];
        $attrTmps = self::dbConn(self::$dbAttr)
            ->where('del', 0)
            ->where('yw_type', 1)
            ->where('yw_id', $id)
            ->get();
        if (!count($attrTmps)) {
            $attrs = ($cateOne&&$cateOne->attr_str) ? explode(',', $cateOne->attr_str) : [];
            foreach ($attrs as $v) {
                $arys[] = [
                    'id' => 0, 'key' => $v, 'val' => '',
                ];
            }
        } else {
            foreach ($attrTmps as $v) {
                $arys[] = [
                    'id' => $v->id, 'key' => $v->attr_name, 'val' => $v->attr_val,
                ];
            }
        }
        if (!count($arys)) {
            return self::toback(-2, '没有属性');
        }
//        self::dbConn(self::$dbYwGoods)
//            ->where('id', $id)
//            ->update(['cate_id' => $cate_id]);
        return self::toback(0, '', $arys);
    }

    //属性新增
    public static function attrAdd($tmpid, $key, $val)
    {
        $attrOne = self::dbConn(self::$dbAttr)
            ->where('del', 0)
            ->where('yw_id', $tmpid)
            ->where('attr_name', $key)
            ->first();
        if (!empty($attrOne)) {
            return self::toback(-2, '属性已存在');
        }
        $lastid = self::dbConn(self::$dbAttr)->insertGetId([
            'yw_type' => 1, 'yw_id' => $tmpid,
            'attr_name' => $key, 'attr_val' => $val,
            'addtime' => toDate(), 'uptime' => toDate(),
        ]);
        if (!$lastid) {
            return self::toback(-2, 'error');
        }
        return self::toback();
    }

    //属性修改
    public static function attrEdit($attrid, $key, $val)
    {
        $attrOne = self::dbConn(self::$dbAttr)
            ->where('del', 0)
            ->where('id', $attrid)
            ->first();
        if (empty($attrOne)) {
            return self::toback(-2, '属性不存在');
        }
        $up = self::dbConn(self::$dbAttr)->where('id', $attrid)
            ->update([
                'attr_name' => $key, 'attr_val' => $val, 'uptime' => toDate(),
            ]);
        if (!$up) {
            return self::toback(-2, 'error');
        }
        return self::toback();
    }

    //属性删除
    public static function attrDel($attrid)
    {
        $attrOne = self::dbConn(self::$dbAttr)
            ->where('del', 0)
            ->where('id', $attrid)
            ->first();
        if (empty($attrOne)) {
            return self::toback(-2, '属性不存在');
        }
        $up = self::dbConn(self::$dbAttr)->where('id', $attrid)
            ->update([
                'del' => 1, 'deltime' => toDate(),
            ]);
        if (!$up) {
            return self::toback(-2, 'error');
        }
        return self::toback();
    }
}